﻿#if !LITE

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;

using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Core = Microsoft.Office.Core;
using iTextSharp.text;


namespace iPdf
{
    class PptConverter: OfficeConverter
    {
        #region Fields
        PowerPoint.Application powerPoint ;
        PowerPoint.Presentation presentation ;
        #endregion

        #region Properties
        protected override string ApplicationName
        {
            get
            {
                return "Microsoft PowerPoint";
            }
        }
        #endregion

        protected override void OpenApplication()
        {
            if (powerPoint == null)
            {
                powerPoint = new PowerPoint.ApplicationClass();
            }
        }

        protected override void CloseApplication()
        {
            if (powerPoint != null)
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(powerPoint);
                powerPoint = null;
            }
        }

        protected override void OpenFile()
        {
            presentation = powerPoint.Presentations.Open(workItem.Source,
                                        Core.MsoTriState.msoTrue, Core.MsoTriState.msoFalse,
                                        Core.MsoTriState.msoFalse);
        }

        protected override void CloseFile()
        {
            if (presentation != null)
            {
                presentation.Close();
                presentation = null;
            }
        }

        protected override void PrintOut()
        {
            PdfCreator.PrintOut(presentation);
        }

        protected override void ExtractBookmarks()
        {
            if (!pageOption.Bookmark || presentation == null)
            {
                return;
            }

            PageSetup pageSetup = new PageSetup(presentation.PageSetup);
            foreach(PowerPoint.Slide slide in presentation.Slides)
            {
                if(!string.IsNullOrEmpty(slide.Name) )
                {
                    string title = string.Empty;
                    try
                    {
                        if (slide.Shapes != null && 
                            slide.Shapes.HasTitle == Microsoft.Office.Core.MsoTriState.msoTrue &&
                            slide.Shapes.Title.TextFrame != null &&
                            slide.Shapes.Title.TextFrame.TextRange != null)
                        {
                            title = slide.Shapes.Title.TextFrame.TextRange.Text;
                        }
                    }
                    catch
                    {
                        title = string.Empty;
                    }

                    if (!string.IsNullOrEmpty(title))
                    {
                        BookmarkNode node = CreateBookmarkByPageNo(
                                                slide.SlideIndex, title, 0, pageSetup);
                        bookmarks.Add(node.Map);
                    }
                }
            }

        }

    }
}

#endif